home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_097 / cutandpaste / spaste.c < prev   
C/C++ Source or Header  |  1992-05-06  |  3KB  |  120 lines

  1. /*
  2.  * Serially paste a file together
  3.  *
  4.  * John Weald
  5.  */
  6. #include <stdio.h>
  7.  
  8. extern void exit();
  9.  
  10.  
  11. spaste(files, c, n)
  12. char *files[];        /* Null terminate list of input files        */
  13. char c[];        /* The concatintaion characters            */
  14. int n;            /* The number of above                */
  15. {
  16.     int i;
  17.     FILE *fp;
  18.  
  19.     if (files[0] == (char *)NULL)
  20.     {
  21.         spfile(stdin, c, n);
  22.         return;
  23.     }
  24.  
  25.     for (i = 0; files[i] != (char *)NULL; i++)
  26.     {
  27.         if (*files[i] == '-')
  28.             fp = stdin;
  29.         else if ((fp = fopen(files[i], "r")) == (FILE *)NULL)
  30.         {
  31.             fprintf(stderr, "Failed to open file %s\n", files[i]);
  32.             exit(1);
  33.         }
  34.         spfile(fp, c, n);
  35.         (void)fclose(fp);
  36.     }
  37. }
  38.     
  39. /* 
  40.  * Do the actual paste of a stream.
  41.  *
  42.  * The method here is to read in the stream and replace all newline
  43.  * characters with concatintaion characters. 
  44.  * Output occurs after each chuck is parsed, or if the concatination character
  45.  * is the null seperator (otherwise puts() would screw up on whole chunk).
  46.  *
  47.  * The stream is read in BUFSIZ chunks using fread. The input buffer is one
  48.  * larger than read, so that it can be null terminated. 
  49.  *
  50.  * When we read in each chunk we must check if it needs to be joined to the
  51.  * previous one i.e. the last character on the last chunk was a newline.
  52.  */
  53. static
  54. spfile(fp, con, ncons)
  55. FILE *fp;        /* serially paste this stream            */
  56. char con[];        /* The concatintaion characters            */
  57. int ncons;        /* The number of above                */
  58. {
  59.     char *pstart;    /* The start of the string            */
  60.     char *ptr;    /* Walks down the stream            */
  61.     char buf[BUFSIZ + 1]; /* To ensure null termination        */
  62.     int n;        /* Number of bytes read with fread()        */
  63.     int k = 0;    /* Index into concatination character array    */
  64.     int join;    /* Join this chunk to the next?            */
  65.     char last;    /* The very last character looked at.        */
  66.  
  67.  
  68.     join = 0;
  69.     while ((n = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) != 0)
  70.     {
  71.         if (join)
  72.         {
  73.             /* Join with last chunk */
  74.             putchar(con[k]);
  75.             k = (k + 1) % ncons;
  76.             join = 0;
  77.         }
  78.         /* Join to next chunk? */
  79.         if (buf[n-1] == '\n')
  80.         {
  81.             join++;
  82.             /* Ignore the newline */
  83.             n--;
  84.         }
  85.  
  86.         /* ensure null terminated buffer */
  87.         buf[n] = '\0';
  88.  
  89.         
  90.         /* 
  91.           * walk thru this chunk 
  92.          * replace all newlines with the next concat. char.
  93.          */
  94.         for (pstart = ptr = buf; *ptr != '\0'; ptr++)
  95.         {
  96.             if (*ptr == '\n')
  97.             {
  98.                 *ptr = con[k];
  99.                 if (con[k] == '\0')
  100.                 {
  101.                     fputs(pstart, stdout);
  102.                     pstart = ptr + 1;
  103.                 }
  104.                 k = (k + 1) % ncons;
  105.             }
  106.         }
  107.         fputs(pstart, stdout);
  108.  
  109.         last = *(ptr - 1);
  110.     }
  111.  
  112.     /*
  113.      * Maybe they asked for the newline as the
  114.      * concatination char. We would hate to give them two newlines
  115.      * in a row.
  116.      */
  117.     if (last != '\n')
  118.         putchar('\n');
  119. }
  120.